/*
 * @Author: huangjinyao huangjinyao@hjy.cn
 * @Date: 2024-07-03 22:32:54
 * @LastEditors: 朝言夕识 11442050+we-know-each-other-every-day@user.noreply.gitee.com
 * @LastEditTime: 2024-07-06 16:00:00
 * @FilePath: \小兔仙项目\vue-rabbit\src\stores\cartStore.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { useUserStore } from './userStore'
import { insertCartAPI, findNewCartListAPI, delCartAPI } from '@/apis/cart'


// 购物车数据
export const userCarStore = defineStore("cart", () => {

    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    // 声明一个响应式的cartList变量
    const cartList = ref([]);

    const updateNewList = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.result
    }

    // 定义一个函数，用于添加商品到购物车
    const addCart = async (goods) => {
        const { skuId, count } = goods
        if (isLogin.value) {
            //登录之后的加入购物车逻辑
            insertCartAPI({ skuId, count }).then(res => {
                console.log(res)
                updateNewList().then(res => {
                    console.log(res)
                })
            })


        } else {
            // 在cartList中查找商品skuId相同的商品
            const item = cartList.value.find((item) => goods.skuId === item.skuId)
            // 如果找到了，则将商品的数量加1
            if (item) {
                item.count = item.count + goods.count;
            } else {
                // 如果没有找到，则将商品添加到cartList中
                cartList.value.push(goods);
            }
        }

    }



    // 定义一个函数，用于从购物车中删除商品
    const delCart = async (skuId) => {

        if (isLogin.value) {
            await delCartAPI([skuId]).then(res=>{
                console.log(res)
                updateNewList()
            })
           
        } else {
            // 在cartList中查找商品skuId相同的商品的索引
            const idx = cartList.value.findIndex((item) => item.skuId === skuId)
            // 删除商品
            cartList.value.splice(idx, 1)
        }

    }
    //清空购物车
    const clearCart = () => {
        cartList.value = []
    }

    const singleCheck = (skuId, selected) => {
        const item = cartList.value.find((item) => skuId === item.skuId)
        item.selected = selected
    }

    const allCheck = (selected) => {
        cartList.value.forEach(item => item.selected = selected)
    }

    // 计算属性，用于计算购物车中商品的总数量和总价格
    const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
    const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))

    const isAll = computed(() => cartList.value.every(item => item.selected))

    //已选数量和商品价格
    const selectedCount = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count, 0))
    const selectedPrice = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price, 0))

    // 返回购物车相关的函数
    return {
        cartList,
        addCart,
        delCart,
        allCount,
        allPrice,
        clearCart,
        singleCheck,
        isAll,
        allCheck,
        selectedCount,
        selectedPrice,
        updateNewList
    }
}, { persist: true }
)